<%
   go_config = scope[:go_config]
   go_config_revision = scope[:go_config_revision]
   time = 'Unknown'
   config_modified_by = 'Unknown'
   if go_config_revision
       config_modified_time = go_config_revision.getTime().to_s
       time = "#{time_ago_in_words(Time.parse(config_modified_time))} #{'ago'}"
       config_modified_by = go_config_revision.getUsername()
   end
-%>
<%= form_for go_config, :as => :go_config, :url => config_update_path, :html => {:id => "config_editor_form", :method => :put} do |form| %>
    <div class='form_heading'>
        <div class="buttons-group">
            <div class="config_change_timestamp" title="<%= "#{'Last modified'}: #{(time)} #{'by'} #{(config_modified_by)}" -%>"><%= "#{'Last modified'}: #{(time)} #{'by'} #{(config_modified_by)}" -%></div>
            <%= link_to('Cancel', config_view_path, :class => "link_as_button", :id => "cancel_edit") %>
            <%= form.submit('SAVE', :class => "link_as_button primary" , :id => "save_config", :disabled => 'disabled') -%>
        </div>
    </div>
    <% if @render_config_via_ajax %>
      <div id="content_area" style="height: 400px;">
        <div class="spinner-container" style="
          position:      relative;
          top:           30%;
          left:          50%;
          border:        16px solid #f3f3f3;
          border-top:    16px solid #963fb7;
          border-radius: 50%;
          width:         100px;
          height:        100px;
          margin:        -50px 0 0 -50px;
          animation:     spin 2s linear infinite;
        "></div>
          <%= form.text_area html_escape(:content), value: nil, :size => '100%x100%', :class => "code" ,:id => "content", :autocomplete => 'off', :autocorrect => 'off', :autocapitalize => 'off', :spellcheck => "false", 'data-config-url' => "/go/api/admin/config/#{@go_config.md5}.xml" , style: 'visibility: hidden; display: none;'%>
      </div>
      <script type="text/javascript">
        jQuery(function () {
          var httpRequest = new XMLHttpRequest();
          var textArea = document.querySelector('#content_area textarea.code');
          httpRequest.open('GET', textArea.getAttribute('data-config-url'), true);
          httpRequest.send();
          httpRequest.onreadystatechange = function () {
            if (httpRequest.readyState === XMLHttpRequest.DONE) {
              if (httpRequest.status === 200) {
                jQuery("input#save_config").removeAttr("disabled");
                textArea.value = httpRequest.responseText;
              } else {
                textArea.value = "There was an error fetching the config xml. Please try again in some time. If the error persists, contact the GoCD server administrator for help.";
              }

              // finally remove the style on the text area to make it visible.
              document.querySelector('#content_area .spinner-container').remove();
              document.querySelector('#content_area').removeAttribute('style');
              textArea.removeAttribute('style');
            }
          };
        });
      </script>
    <% else %>
      <div id="content_area">
        <%= form.text_area html_escape(:content), :size => '100%x100%', :class => "code" ,:id => "content", :autocomplete => 'off', :autocorrect => 'off', :autocapitalize => 'off', :spellcheck => "false", 'data-config-url' => "/go/api/admin/config/#{@go_config.md5}.xml" %>
      </div>
      <script type="text/javascript">
        jQuery(document).ready(function() {
          jQuery("input#save_config").removeAttr("disabled");
        });
      </script>
    <% end %>

    <%= form.hidden_field :md5 %>

<% end %>


